<?php

// 网站配置表

namespace app\console\model;

use app\index\controller\Index;
use app\index\controller\MemberMessage;
use think\Cache;
use think\Db;
use think\Model;
use think\Request;

class Config extends Model
{
    /*
     * 配置项更新数据
     */
    public static function renew()
    {
        $data = Request::instance()->param();

        if (!empty($data)) {

            // 缩略图
//            if (!empty($_FILES['web_markpicurl']['name'])) {
//                $file = Request::instance()->file('web_markpicurl');
//                // 移动到框架应用根目录/public/uploads/ 目录下
//                $file->move(ROOT_PATH . 'uploads', 'thumb_watermarket');
//
//            }
//
//            // 天天特价
//            if (!empty($_FILES['web_special_price_pic']['name'])) {
//
//                $file = Request::instance()->file('web_special_price_pic');
//                // 移动到框架应用根目录/public/uploads/ 目录下
//                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'home_page');
//                db('config')->where('varname','web_special_price1')->setField('varvalue',$info->getSaveName());
//
//
//            }
//
//            // 热卖榜
//            if (!empty($_FILES['web_hot_selling_pic']['name'])) {
//                $file = Request::instance()->file('web_hot_selling_pic');
//                // 移动到框架应用根目录/public/uploads/ 目录下
//                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'home_page');
//                db('config')->where('varname','web_hot_selling1')->setField('varvalue',$info->getSaveName());
//            }
//
//            // 品牌街
//            if (!empty($_FILES['web_brand_street_pic']['name'])) {
//                $file = Request::instance()->file('web_brand_street_pic');
//                // 移动到框架应用根目录/public/uploads/ 目录下
//                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads' . DS . 'home_page');
//                db('config')->where('varname','web_brand_street1')->setField('varvalue',$info->getSaveName());
//            }


            //dump($data);
            foreach ($data as $key => $value) {
                if($key =="web_draw"){

                    //如果是开启
                    if($value =="Y"){
                        $list = Db::name('apply_log')->where('status',1)->select();
                        foreach ($list as $item){
                            Db::name('apply_log')->where('id',$item['id'])->setField('status',2);
                            (new MemberMessage)->jpushMessage($item['member_id'],'可以抽奖啦!','可以抽奖啦!',4);
                        }
                    }

                }
                self::where(['varname' => $key])->setField(['varvalue' => $value]);
            }

            //Cache::rm('web');
            return true;
        }
        return false;
    }


    /*
     * 添加新变量
     */
    public static function newVariable()
    {
        $data = Request::instance()->param();

        // 验证器
        $validate = validate('Config');
        if (!$validate->check($data)) {

            fail($validate->getError());

        }

        $data['varname'] = 'web_' . $data['varname'];
        $data['create_time'] = date('Y-m-d H:i:s', time());

        $status = (new Config())->save($data);

        if (!empty($status)) {
            Cache::rm('web');
            return true;
        }
        return false;

    }
}